package cn.shiwei.hr.service.impl;

import cn.shiwei.hr.domain.KillOrder;
import cn.shiwei.hr.dto.KillOrderDto;
import cn.shiwei.hr.mapper.KillOrderMapper;
import cn.shiwei.hr.repository.AssertUtil;
import cn.shiwei.hr.service.IKillOrderService;
import cn.shiwei.hr.to.TempOrderTo;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import static cn.shiwei.hr.constants.KillCourseConstants.CACHE_ORDER;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author 1363732197@qq.com
 * @since 2021-12-11
 */
@Service
public class KillOrderServiceImpl extends ServiceImpl<KillOrderMapper, KillOrder> implements IKillOrderService {

    @Autowired
    private RedisTemplate redisTemplate;

    @Override
    public void placeAnOrder(KillOrderDto killOrderDto) {

        // 做非空校验, 用@Valid
        String orderNo = killOrderDto.getOrderNo();
        Long loginId = 1L;
        // 因该从security中取出
        // SecurityContextHolder.getContext().getAuthentication().getName();

        // 从redis中取出临时订单
        TempOrderTo  tempOrderTo = (TempOrderTo) redisTemplate.opsForValue().get(String.format(CACHE_ORDER, loginId, orderNo));
        AssertUtil.isNull(tempOrderTo, "无此订单 或 订单已过期!");

        KillOrder killOrder = new KillOrder();
        BeanUtils.copyProperties(tempOrderTo, killOrder);

    }
}
